#ifndef _APP_UPPERPATA_H_
#define _APP_UPPERPATA_H_

#include "sys_hc32f460.h"




#define XG_MSG_HEADER_L						0xAA//协议头低字节
#define XG_MSG_HEADER_H						0xBB//协议头高字节

#define XG_CMD_CODE_READ					0x01//读功能
#define XG_CMD_CODE_WRITE					0x02//写功能

#define XG_BATCURR_OFFSET 1000000				//电流偏移
#define XG_BATTEMP_OFFSET 128					//温度偏移

//数据组地址
typedef enum {
	realDataGrpAddr  = 0x01,					//实时数据地址
	sysParamGrpAddr  = 0x02,					//系统参数地址
	ctrlDataGrpAddr  = 0x03,					//控制数据地址
	caliDataGrpAddr  = 0x04,					//校准数据地址
	errRecordGrpAddr = 0x05,					//故障记录地址
	errRecordLogAddr = 0x06,                    //故障记录日志
}XgDataGrpAddr;


//故障日志读取
typedef enum{
	readFirstLog = 0x01,  //读取第一条
	readNextLog = 0x02,   //读取下一条

}XgErrTsdbLog;

//控制对象
typedef enum {
	preChargeSwCtrl = 0x01,						//预充开关
	chargeSwCtrl    = 0x02,						//充电开关
	dschargeSwCtrl  = 0x03,						//放电开关
	balanceSwCtrl   = 0x04,						//均衡开关
	heatSwCtrl      = 0x05,						//加热开关
	otaUpdateCtrl   = 0x06,						//OTA升级开关
	sysStateCtrl    = 0x07,						//系统状态
}XgCtrlObj;

//控制码
typedef enum {
	closeCode       = 0x00,						//关闭
	openCode        = 0x01,						//打开
	rebootCode      = 0x02,						//重启
	sleepCode       = 0x03,						//休眠
	wakeupCode      = 0x04,						//唤醒
}XgCtrlCode;

typedef struct {
	uint8_t ctrlObj;
	uint8_t ctrlCode;
}XgCtrlInfo;

//校准对象
typedef enum {
	currentCali = 0x01,							//电流校准
	bmsTimeCali = 0x02,							//BMS时钟校准
	batVolCali  = 0x03,							//单体采样校准
}XgCaliObj;

typedef struct{
	uint8_t batNum;
	uint16_t stdBatVol;
}XgBatVolCaliValue;

typedef union {
	uint32_t stdCurrent;
	uint8_t stdTimeStr[14];
	XgBatVolCaliValue stdBatValue;
	uint8_t  value[14];
}XgCaliValue;

typedef enum {
	xg_ExeSucc = 0xaa,
	xg_LastData = 0xbb,
	xg_ExeFailed = 0xff,
}XgExcRes;

typedef struct {
	uint8_t  caliObj;
	XgCaliValue	   caliValue;
}XgCaliInfo;

unsigned short XgBmsProtocolCrc(unsigned char *checkArray, unsigned int checkLen);
void parse_xgld_protocol_data(uint8_t *data,uint16_t len);

#endif